<?php

namespace App\Admin\Controllers;

use App\Models\HelpDoc;
use App\Models\HelpWorkOrder;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class HelpCenterController extends Controller
{
    use HasResourceActions;

    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.help_center'))
            ->description('description')
            ->row(function (Row $row) {
                $row->column(8, $this->workOrders());
                $row->column(4, $this->docs());
            });
    }

    public function workOrders()
    {
        $grid = new Grid(new HelpWorkOrder());
        $grid->setTitle(trans('admin.work_order_lists'));
        $grid->model()->where('p_id', 0)->orderBy('id', 'desc');
        $grid->user()->username(trans('admin.username'));
        $grid->user()->nick_name(trans('admin.nick_name'));
        $grid->user()->avatar(trans('admin.avatar'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('content', trans('admin.content'))->limit(10)->modal(trans('admin.reply'), function ($model) {

            $comments = $model->reply()->get()->map(function ($comment) {
                $data = $comment->only(['id', 'content', 'created_at']);

                return $data;
            });

            return new Table(['ID', trans('admin.content'), trans('admin.created_at')], $comments->toArray());
        });
        $grid->images(trans('admin.image'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->actions(function (Grid\Displayers\Actions $actions) use ($grid) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
            $row = $actions->row;
            if ($row->is_close != 1) {
                $actions->append('<a href="' . route('help_reply.edit', ['id' => $row->id]) . '"
                 class="btn btn-xs" title="' . trans('admin.reply') . '"><i class="fa fa-mail-reply "></i></a>');
            }
        });

        $grid->replay_info(trans('admin.reply_status'))->display(function () {
            $last_replay = collect($this->reply)->first();
            if (empty($last_replay) || $last_replay->role == 'user') {
                return '<span class="badge bg-red">' . trans('admin.padding_reply') . '</span>';
            } else {
                return '<span class="badge bg-aqua">' . trans('admin.finished_reply') . '</span>';
            }
        });
        $grid->column('is_close', trans('admin.status'))->using(HelpWorkOrder::$CLOSED_MAP)->badge();
        $grid->column('created_at', trans('admin.created_at'));
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->disableCreateButton();

        return $grid;
    }

//    public function reply($id, Content $content)
//    {
//        return $content
//            ->header(trans('admin.work_order'))
//            ->description(trans('admin.reply'))
//            ->body($this->form()->edit($id));
//    }

//    public function work_order_edit()
//    {
//        $form = new Form(new HelpDoc());
//        $form->text('title', trans('admin.title'));
//        $form->editor('content', trans('admin.content'));
//        $form->number('order', trans('admin.sort'))->default(0);
//
//        return $form;
//    }

    public function docs()
    {
        $grid = new Grid(new HelpDoc());
        $grid->setTitle(trans('admin.help_doc'));
        $grid->model()->orderBy('order', 'asc');
        $grid->column('id');
        $grid->column('title', trans('admin.title'));
        $grid->column('order', trans('admin.sort'))->orderable();
        $grid->disableRowSelector();
        $grid->disablePagination();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->actions(function ($action) {
            $action->disableView();
        });

        return $grid;
    }

    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.edit'))
            ->description(trans('admin.help_doc'))
            ->body($this->form()->edit($id));
    }

    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.create'))
            ->description(trans('admin.help_doc'))
            ->body($this->form());
    }

    protected function form()
    {
        $form = new Form(new HelpDoc());

        $form->text('title', trans('admin.title'));
        $form->editor('content', trans('admin.content'));
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        return $form;
    }
}
